﻿using System;
using System.Collections.Generic;
using System.Linq;
using HotelierChannelManagement.Model;
using HotelierChannelManagement.Model.Entities;
using HotelierChannelManagement.Model.Repository;

public class HotelRepository : RepositoryBase<HoteliersChannelManagementDBEntities>, IHotelRepository
{
    #region IHotelRepository Members

    public Hotel GetHotel(Guid custID)
    {
        using (DataContext)
        {
            return Get<Hotel>(c => c.HotelID == custID);
        }
    }

    public List<Hotel> GetHotels()
    {
        using (DataContext)
        {
            return GetList<Hotel, string>(c => c.HotelName).Take(50).ToList();
        }
    }

    //Can handle insert, update or delete
    public OperationStatus SaveHotel(Hotel Hotel)
    {
        using (DataContext)
        {
            if (Hotel.ChangeTracker.State == ObjectState.Deleted)
            {
                //Handle foreign key deletions using sproc
                return DeleteCustomer(Hotel.HotelID);
            }
            return Save(Hotel);
        }
    }

    private OperationStatus DeleteCustomer(Guid id)
    {
        OperationStatus opStatus;

        using (DataContext)
        {
            opStatus = ExecuteStoreCommand("exec DeleteHotel {0}", id);
        }
        if (opStatus.RecordsAffected == 0)
        {
            opStatus.Status = false;
            opStatus.Message = "Unable to delete Hotel: " + opStatus.Message;
        }
        return opStatus;
    }

    #endregion
}